package best.driver.android.Activity.Base;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.*;
import best.driver.android.AppInfo;
import best.driver.android.BestApp;
import best.driver.android.Map.location.LocationManager;
import best.driver.android.R;
import best.driver.android.Utils.*;
import com.umeng.analytics.MobclickAgent;

import java.util.prefs.Preferences;

public class BaseActivity extends FragmentActivity {

	protected BestApp mApp;
	protected Logger mLogger = Logger.createLogger(getClass().getSimpleName());
	protected ProgressDialogUtil mWaitingDialog;
	protected boolean mIsDestroyed = false;

	/**
	 * 顶部标题栏View(装载mCommonNaviBar) *
	 */
	private View mNaviBar;
	/**
	 * 主界面 *
	 */
	private RelativeLayout mMainView;
	/**
	 * 标题栏左边按钮
	 */
	protected Button mBtnLeft;
	/**
	 * 标题栏右边按钮
	 */
	protected Button mBtnRight;

	ImageView mIvRight;
	/**
	 * 标题栏标题
	 */
	private TextView mTextTitle;
	protected ImageView mIvLeft;
	private TextView mTvTitleDesc;
	private View mViewTopLine;
	protected View view;
	protected RelativeLayout mRootView;

	@SuppressLint("HandlerLeak")
	protected Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
				//case AppInfo.SERVER_BACK_CODE_ERROR:
				case 1:
					hideWaiting();
					//AppInfo.showToast(getApplicationContext(), getString(R.string.check_network));
					break;
				default:
					handlerEDMessage(msg);
					break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// 标题栏隐藏
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		mWaitingDialog = new ProgressDialogUtil(this);
		mApp = (BestApp) getApplication();
		mRootView = (RelativeLayout) LayoutInflater.from(this).inflate(
				R.layout.layout_base_ui, null);
		setContentView(mRootView);

		mNaviBar = findViewById(R.id.navi_bar);
		mMainView = (RelativeLayout) findViewById(R.id.main_view);

		mBtnLeft = (Button) findViewById(R.id.btnLeft);
		mBtnRight = (Button) findViewById(R.id.btnRight);
		mIvLeft = (ImageView) findViewById(R.id.iv_left);
		mIvRight = (ImageView) findViewById(R.id.iv_right);
		mTextTitle = (TextView) findViewById(R.id.title);
		mViewTopLine = findViewById(R.id.view_top_line);
		mTvTitleDesc = (TextView) findViewById(R.id.tv_title_desc);
		mIvLeft.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				onBack();
			}
		});
        mIvRight.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                onclickRightIv();
            }
        });
		mApp.addActivity(this);
	}

	protected void onBack() {
		finish();
	}

    protected void onclickRightIv() {

    }

	public Button getBtnRight() {
		return mBtnRight;
	}

	@Override
	protected void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}

	@Override
	protected void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
		Utils.hideKeyboard(BaseActivity.this);
	}

	@Override
	protected void onStart() {
		BackgroundUtils.getInstance().dealAppRunState("", true);
		super.onStart();
	}

	@Override
	protected void onStop() {
		BackgroundUtils.getInstance().dealAppRunState();
		super.onStop();
	}

	@Override
	protected void onDestroy() {
		mIsDestroyed = true;
		super.onDestroy();
		mApp.removeActivity(this);
	}

	/**
	 * 设置左右按钮文字,如果参数为null，则不显示该按钮
	 *
	 * @param leftStr
	 *            左边按钮文字
	 * @param rightStr
	 *            右边按钮文字
	 */
	protected void setBtnText(String leftStr, String rightStr) {
		if (!TextUtils.isEmpty(leftStr)) {
			mBtnLeft.setVisibility(View.VISIBLE);
			mBtnLeft.setText(leftStr);
		} else {
			mBtnLeft.setVisibility(View.GONE);
		}
		if (!TextUtils.isEmpty(rightStr)) {
			mBtnRight.setVisibility(View.VISIBLE);
			mBtnRight.setText(rightStr);
		} else {
			mBtnRight.setVisibility(View.GONE);
		}
	}

	protected void setTitleDesc(String desc) {
		if(!TextUtils.isEmpty(desc)) {
			mTvTitleDesc.setVisibility(View.VISIBLE);
            mTvTitleDesc.setText(desc);
		} else {
            mTvTitleDesc.setVisibility(View.GONE);
        }
	}

	protected void setTopLineVisible(boolean visible) {
		mViewTopLine.setVisibility(visible ? View.VISIBLE : View.GONE);
	}

	protected void setRightIvBak(int id) {
		mIvRight.setImageResource(id);
		mIvRight.setVisibility(View.VISIBLE);
	}

	protected void setLeftBtnBak(int id) {
		mBtnLeft.setBackgroundResource(id);
	}

	protected void setLeftIvBak(int id) {
		mIvLeft.setImageResource(id);
		mIvLeft.setVisibility(View.VISIBLE);
	}

	protected void setView(View view) {
		RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.MATCH_PARENT,
				RelativeLayout.LayoutParams.MATCH_PARENT);
		mMainView.addView(view, params);
	}

	protected void hideTopBar() {
		mNaviBar.setVisibility(View.GONE);
	}

	protected void showTopBar() {
		mNaviBar.setVisibility(View.VISIBLE);
	}

	/**
	 * ************************************* 设置标题栏标题
	 *
	 * @param title
	 *            标题文字
	 */
	protected void setTopTopic(String title) {
		if (!TextUtils.isEmpty(title)) {
			mTextTitle.setVisibility(View.VISIBLE);
			mTextTitle.setText(title);
		}
	}

	protected void setView(int viewLayout) {
		view = LayoutInflater.from(this).inflate(viewLayout, null);
		setView(view);
	}

	protected void handlerEDMessage(Message msg) {

	}

	public void showWaiting(String message) {
		if (mWaitingDialog != null) {
			mWaitingDialog.show(message);
		}
	}

	public void showWaiting() {
		if (mWaitingDialog != null) {
			mWaitingDialog.show();
		}
	}

	public void showWaitingNotCancelable(String message) {
		if (mWaitingDialog != null) {
			mWaitingDialog.show(message, false);
		}
	}

	public void showWaitingReturnLastActivity(String message) {
		if(mWaitingDialog != null) {
			//mWaitingDialog.showReturn(message);
		}
	}

	public void hideWaiting() {
		if (mWaitingDialog != null) {
			mWaitingDialog.hide();
		}
	}

	public Activity getActivity() {
		return this;
	}

	public boolean destroyed() {
		return mIsDestroyed;
	}

	protected int getColor(int resourceId) {
		return getResources().getColor(resourceId);
	}

}
